﻿@{

    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <form class="layui-form layui-row layui-col-space16">
        <div class="layui-col-md4">
            <input type="text" name="measureCode" value="" placeholder="单位编码" class="layui-input" lay-affix="clear" width="100">
        </div>


        <div class="layui-col-md4">

            <div class="layui-input-wrap">
                <input type="text" name="measureName" placeholder="单位昵称" lay-affix="clear" class="layui-input" width="100">
            </div>
        </div>

        <div class="layui-col-md4">
            <button class="layui-btn" lay-submit lay-filter="demo-table-search">搜索</button>
            <button type="button" class="layui-btn layui-btn-primary" lay-on="test-page-custom">
                <span></span> 添加
            </button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </form>
    <table class="layui-hide" id="ID-table-demo-search"></table>

    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    <script src="//unpkg.com/layui@2.9.17/dist/layui.js"></script>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script type="text/html" id="toolDemo">
        <div class="layui-clear-space">
          <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
          <a class="layui-btn layui-btn-xs" lay-event="more">删除</a>
        </div>
    </script>
    <script>
        layui.use(function () {

            var table = layui.table;
            var form = layui.form;
            var laydate = layui.laydate;
            function getShow() {
                // 创建表格实例
                table.render({
                    elem: '#ID-table-demo-search',
                    url: '/api/app/measuring-unit/measuring-unit', // 此处为静态模拟数据，实际使用时需换成真实接口
                    cols: [[

                        { field: 'MeasureCode', fixed: 'left', title: '单位编码', sort: true, totalRow: '合计：' },
                        { field: 'MeasureName', title: '单位名称' },
                        { field: 'PrimaryFlag', title: '是否是主单位 <i class="layui-icon layui-icon-tips layui-font-14" lay-event="email-tips" title="该字段开启了编辑功能" style="margin-left: 5px;"></i>', fieldTitle: '是否是主单位', hide: 0, width: 100, expandedMode: 'tips', edit: 'text' },
                        { field: 'ChangeRate', width: 100, title: '与主单位换算比例', sort: true },
                        { field: 'EnableFlag', title: '是否启用', },
                        { field: 'Remark', title: '备注', sort: true, totalRow: '{{= d.TOTAL_NUMS }} 😊' },

                        { fixed: 'right', title: '操作', width: 134, minWidth: 125, templet: '#toolDemo' }
                    ]],
                    page: false,
                    height: 510
                });
            }
            getShow();
            // 日期
            laydate.render({
                elem: '.demo-table-search-date'
            });
            // 搜索提交
            form.on('submit(demo-table-search)', function (data) {
                var field = data.field; // 获得表单字段
                // 执行搜索重载
                table.reload('ID-table-demo-search', {
                    page: {
                        curr: 1 // 重新从第 1 页开始
                    },
                    where: {
                        measureCode: field.measureCode,
                        measureName: field.measureName,
                    } // 搜索的字段
                });
                layer.msg('搜索成功<br>此处为静态模拟数据，实际使用时换成真实接口即可');
                return false; // 阻止默认 form 跳转
            });
        });
    </script>
    <script>
        layui.use(function () {
            var $ = layui.$;
            var layer = layui.layer;
            var util = layui.util;
            var form = layui.form;
            // 事件
            util.on('lay-on', {

                'test-page-custom': function () {
                    layer.open({
                        type: 1,
                        area: '500px',
                        resize: false,
                        shadeClose: true,
                        title: '添加单位',
                        content: `
                                                  <form class="layui-form" action="">
                                  <div class="layui-form-item">
                                    <label class="layui-form-label">单位编码</label>
                                    <div class="layui-input-block">
                                              <input type="text" name="measureCode" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                                    </div>
                                  </div>
                                           <div class="layui-form-item">
                                            <label class="layui-form-label">单位名称</label>
                                            <div class="layui-input-block">
                                                      <input type="text" name="measureName" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
                                            </div>
                                          </div>

                                  <div class="layui-form-item">
                                    <label class="layui-form-label">是否为主单位</label>
                                    <div class="layui-input-block">
                                              <input type="radio" name="primaryFlag" value="Y" title="是" checked>
                                              <input type="radio" name="primaryFlag" value="N" title="否">
                                    </div>
                                             <div class="layui-form-item">
                                            <label class="layui-form-label">是否启用</label>
                                            <div class="layui-input-block">
                                                      <input type="radio" name="enableFlag" value="Y" title="是" checked>
                                                      <input type="radio" name="enableFlag" value="N" title="否">
                                            </div>
                                  </div>
                                          <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label">备注</label>
                                    <div class="layui-input-block">
                                              <textarea placeholder="请输入内容" class="layui-textarea" name="remark"></textarea>
                                    </div>
                                  </div>
                                  <div class="layui-form-item">
                                    <div class="layui-input-block">
                                      <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
                                      <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                    </div>
                                  </div>
                                </form>
                                        `,
                        success: function () {
                            // 对弹层中的表单进行初始化渲染
                            form.render();
                            // 表单提交事件
                            form.on('submit(demo1)', function (data) {
                                var field = data.field; // 获取表单字段值
                                // 显示填写结果，仅作演示用
                                // layer.alert(JSON.stringify(field), {
                                //     title: '当前填写的字段值'
                                // });
                                var input = {
                                    measureCode: field.MeasureCode,
                                    measureName: field.measureName,
                                    primaryFlag: field.primaryFlag,
                                    primaryId: field.primaryId,
                                    changeRate: field.changeRate,
                                    enableFlag: field.enableFlag,
                                    remark: field.remark,
                                }
                                // 此处可执行 Ajax 等操作
                                // …
                                $.ajax({
                                    url: "/MainData/AddMeasuringUnit",
                                    type: "get",
                                    async: true,
                                    data: field,
                                    contentType: "application/json",
                                    success: res => {
                                        if (res != null) {
                                            alert("添加成功")
                                            layer.close(layer.index);
                                            window.location.href = "/MainData/MeasuringUnit/"
                                            return;
                                        }
                                        else {
                                            console.log(res.code)
                                            alert("添加失败")
                                        }
                                    }
                                });
                                //  console.log("1")
                                return false; // 阻止默认 form 跳转
                            });
                        }
                    });
                }
            })
        });
    </script>
    @* 修改 *@
    <script>
        layui.use('table', function () {
            var $ = layui.$;
            var layer = layui.layer;
            var util = layui.util;
            var form = layui.form;


            var layer = layui.layer;
            var util = layui.util;

            var table = layui.table;
            var form = layui.form;
            var laydate = layui.laydate;
            var laypage = layui.laypage;
            // 事件
            //表单操作
            table.on('tool(ID-table-demo-search)', function (obj) {

                if (obj.event === 'edit') {
                    var data = obj.data;
                    var ytiu = data;
                    console.log(obj.data);
                    MeasureCode=data.MeasureCode;
                    MeasureName=data.MeasureName;
                    PrimaryFlag=data.PrimaryFlag;
                    PrimaryId=data.PrimaryId;
                    EnableFlag=data.EnableFlag;
                    Remark=data.Remark;
                    Id=data.Id;
                    layer.open({
                        type: 2, // page 层类型
                        area: ['500px', '500px'],
                        title: '修改车间',
                        shade: 0.6, // 遮罩透明度
                        shadeClose: true, // 点击遮罩区域，关闭弹层
                        maxmin: true, // 允许全屏最小化
                        anim: 0, // 0-6 的动画形式，-1 不开启
                        content: "/Li/MeasuringUnitUpdate",
                        end: function () {
                            window.location.href = "/MainData/MeasuringUnit/";
                        }
                    });
                }

                else if (obj.event === 'more') {
                    if (!confirm('确定删除吗？')) return;
                    $.ajax({
                        url: '/MainData/DeleteMeasuringUnit', // 替换为你的API URL
                        type: 'GET',
                        data: { id: obj.data.Id },
                        dataType: 'json',
                        success: function (res) {
                            if (res != null) {
                                alert("删除成功");
                                window.location.href = "/MainData/MeasuringUnit/"
                            }
                        }
                    });
                }
            })
        });
    </script>

</body>
</html>
